//
//  DeviceSubViewCon.swift
//  testapihr
//
//  Created by Tek on 2023/6/29.
//

import UIKit
import DatePickerDialog


class DeviceSubViewCon: UIViewController {
    
    
    lazy var dateFmt: DateFormatter = {
        let df = DateFormatter()
        df.dateFormat = "HH:mm"
        df.locale = Locale.init(identifier: "en_US_POSIX")
        return df
    }()
    
    
    lazy var starTimeLabel: UIButton = {
        let txtF = UIButton(frame: .zero)
        txtF.setTitleColor(.black, for: .normal)
        txtF.setTitle("00:00", for: .normal)
        txtF.tag = 0
        txtF.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
        return txtF
    }()
    lazy var endTimeLabel: UIButton = {
        let txtF = UIButton(frame: .zero)
        txtF.setTitleColor(.black, for: .normal)
        txtF.setTitle("00:00", for: .normal)
        txtF.tag = 1
        txtF.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
        return txtF
    }()
    
    lazy var disturbSwitch: UISwitch = {
        let swt = UISwitch(frame: .zero)
        return swt
    }()
    
    lazy var saveBtn: TestBtn = {
        let btn = TestBtn(frame: CGRect.zero, title: "保存") { [weak self] _ in
            self?.handleCallBack()
        }
        return btn
    }()
    
    lazy var cancelbtn: TestBtn = {
        let btn = TestBtn(frame: CGRect.zero, title: "取消") { [weak self] _ in
            self?.dismiss(animated: true)
        }
        return btn
    }()
    
    var date1 = Date()
    var date2 = Date()
    
    typealias SELBLoc = (UInt8, UInt8, UInt8, UInt8, Bool) -> Void
    
    
    var callBack: SELBLoc?
    
    override func viewDidLoad() {
        self.view.backgroundColor = .white
        starTimeLabel.frame = CGRectMake(20, 20, 150, 50)
        endTimeLabel.frame = CGRectMake(170, 20, 150, 50)
        disturbSwitch.frame = CGRectMake(40, 100, 80, 40)
        
        saveBtn.frame = CGRectMake(20, 150, 80, 80)
        cancelbtn.frame = CGRectMake(170, 150, 80, 80)
        
        view.addSubview(starTimeLabel)
        view.addSubview(endTimeLabel)
        view.addSubview(disturbSwitch)
        view.addSubview(saveBtn)
        view.addSubview(cancelbtn)
    
    }
    
    func handleCallBack() {
        
        let cal = Calendar.current.dateComponents(in: .current, from: date1)
        let cal2 = Calendar.current.dateComponents(in: .current, from: date2)
        self.callBack?(UInt8(cal.hour ?? 0),UInt8(cal.minute ?? 0),UInt8(cal2.hour ?? 0),UInt8(cal2.minute ?? 0),disturbSwitch.isOn)
        self.dismiss(animated: false)
    }
    
    @objc
    func btnClick(sender: UIButton) {
        if (sender.tag == 0) {
            let sStr =  starTimeLabel.title(for: .normal) ?? "00:00"
            let d1 = dateFmt.date(from: sStr) ?? Date()
            DatePickerDialog(locale:Locale.current).show("开始时间", doneButtonTitle: "确定", cancelButtonTitle: "取消", defaultDate: d1, datePickerMode: .time) { date in
                    if let dt = date {
                        self.date1 = dt
                        self.starTimeLabel.setTitle(self.dateFmt.string(from: dt), for: .normal)
                    }
                }
        }
        else {
            let eStr =  endTimeLabel.title(for: .normal) ?? "00:00"
            let d2 = dateFmt.date(from: eStr) ?? Date()
            DatePickerDialog(locale:Locale.current).show("结束时间", doneButtonTitle: "确定", cancelButtonTitle: "取消",defaultDate: d2, datePickerMode: .time) { date in
                    if let dt = date {
                        self.date2 = dt
                        self.endTimeLabel.setTitle(self.dateFmt.string(from: dt), for: .normal)
                    }
                }
        }
    }
    
    
    
    
}
